package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.ZyBuilding;
import com.blb.zycommunity.entity.ZyCommunity;
import com.blb.zycommunity.entity.ZyUnit;
import com.blb.zycommunity.service.impl.ZyBuildingServiceImpl;
import com.blb.zycommunity.service.impl.ZyCommunityServiceImpl;
import com.blb.zycommunity.service.impl.ZyUnitServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 单元  前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */

@CrossOrigin
@RestController
public class ZyUnitController {
    @Autowired
    private ZyUnitServiceImpl zyUnitService;

    @Resource
    private ZyCommunityServiceImpl zyCommunityService;

    @Resource
    private ZyBuildingServiceImpl zyBuildingService;
    //单元信息集合
    @GetMapping("/zyUnit-list")
    public ResponseResult<List<ZyUnit>> selectByAll(@RequestParam(required = false,defaultValue = "1")Long current,
                                                    @RequestParam(required = false,defaultValue = "6")Long size,
                                                    @RequestParam(required = false,defaultValue = "")Long communityId,
                                                    @RequestParam(required = false,defaultValue = "")String unitName,
                                                    @RequestParam(required = false,defaultValue = "")String unitCode,
                                                    @RequestParam(required = false,defaultValue = "")String unitHaveElevator,
                                                    @RequestParam(required = false,defaultValue = "")String buildingName
    ){
        IPage<ZyUnit> page = zyUnitService.selectByAll(current, size,unitName,unitCode,unitHaveElevator,buildingName,communityId);
        return ResponseResult.ok(page);
    }
    //    楼栋
    @GetMapping("/zyBuilding")
    public ResponseResult findAll(){
        List<ZyBuilding> zyBuildings = zyBuildingService.list();
        return ResponseResult.ok(zyBuildings);
    }
    //    小区
    @GetMapping("/zyCommunity")
    public ResponseResult findAlla(){
        List<ZyCommunity> zyCommunities = zyCommunityService.list();
        return ResponseResult.ok(zyCommunities);
    }

    //单元添加
    @PostMapping("/zyUnit-add")
    public ResponseResult addUser(@RequestBody ZyUnit zyUnit){
        zyUnit.setCreateTime(LocalDateTime.now());
        zyUnitService.save(zyUnit);
        return ResponseResult.ok("ok");
    }

    //单元修改
    @PutMapping("/zyUnit-update")
    public ResponseResult updateZyUnit(@RequestBody ZyUnit zyUnit){
        zyUnitService.updateById(zyUnit);
        return ResponseResult.ok("ok");
    }
    //    //单元删除
//    @DeleteMapping("/deleteUnitId/{unitId}")
//    public ResponseResult deleteById(@PathVariable Long unitId){
//        zyUnitService.removeById(unitId);
//        return ResponseResult.ok("ok");
//    }
    //批量删除
    @DeleteMapping("/deleteUnitIds/{unitIds}")
    public ResponseResult deleteByIds(@PathVariable Long[] unitIds){
        for (Long unitId : unitIds) {
            zyUnitService.removeById(unitId);
        }
        return ResponseResult.ok("ok");
    }

}
